package com.mingte.manghe.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateTimeUtils {

    public static final long ONE_DAY_MILL_SECONDS = 24 * 60 * 60 * 1000;

    public static int calcDaysLeft(Date fromDt, Date toDt) {
        long from = fromDt.getTime() / ONE_DAY_MILL_SECONDS;
        long to = toDt.getTime() / ONE_DAY_MILL_SECONDS;
        if (from == to) {
            return 0;
        } else if (from > to) {
            return -1;
        }
        int days = (int) (to - from);
        return days;
    }

    public static Date addMonth(Date from, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(from);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }

    public static Date addHour(Date from, int hours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(from);
        cal.add(Calendar.HOUR_OF_DAY, hours);
        return cal.getTime();
    }

    public static Date addDays(Date from, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(from);
        cal.add(Calendar.DAY_OF_WEEK, days);
        return cal.getTime();
    }

    public static int calcDaysDuration(Date d1, Date d2) {
        long timeDiff = d2.getTime() - d1.getTime();
        long days = timeDiff / ONE_DAY_MILL_SECONDS;
        return (int) days;
    }

    public static Date now() {
        return new Date();
    }

    public static String getNow(String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date());
    }

    public static Date getTodayStartTime() {
        Calendar start = Calendar.getInstance();
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        return start.getTime();
    }

    public static Date getTodayEndTime() {
        Calendar start = Calendar.getInstance();
        start.set(Calendar.HOUR_OF_DAY, 23);
        start.set(Calendar.MINUTE, 59);
        start.set(Calendar.SECOND, 59);
        return start.getTime();
    }

    /**
     * 月份增加
     * @param addMonth
     * @return
     */
    public static Date getAddMonth(BigDecimal addMonth) {
        Calendar cal = Calendar.getInstance();
        int currMonth = cal.get(Calendar.MONTH);
        int addMonths = addMonth.intValue();
        if((currMonth + addMonths) > 11){//月份默认从0开始,所以一年总月份是11
            int currYear = cal.get(Calendar.YEAR);
            int addYead = (currMonth + addMonths) / 11;
            int addMonth1 = (currMonth + addMonths) % 11;
            cal.set(Calendar.YEAR,(currYear + addYead));
            cal.set(Calendar.MONTH,addMonth1 - 1);
        }else{
            cal.set(Calendar.MONTH,(currMonth + addMonths));
        }
        return cal.getTime();
    }

    /**
     * 获取输入日期得00：00：00
     * @param date
     * @return
     */
    public static Date getStartTime(Date date) {
        Calendar start = Calendar.getInstance();
        start.setTime(date);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        return start.getTime();
    }

    /**
     * 获取输入日期得23：59：59
     * @param date
     * @return
     */
    public static Date getEndTime(Date date) {
        Calendar start = Calendar.getInstance();
        start.setTime(date);
        start.set(Calendar.HOUR_OF_DAY, 23);
        start.set(Calendar.MINUTE, 59);
        start.set(Calendar.SECOND, 59);
        return start.getTime();
    }

    /**
     * 获取输入月份开始时间 精确到时分秒
     * 例如：2021-07-01 00:00:00
     * @return
     */
    public static Date getInputMonthStartTime(int mouthDiff) {
        Calendar start = Calendar.getInstance();
        start.set(Calendar.MONTH,mouthDiff);
        start.set(Calendar.DAY_OF_MONTH, 1);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        return start.getTime();
    }

    /**
     * 获取当月开始的时间 精确到时分秒
     * 例如：2021-07-01 00:00:00
     * @return
     */
    public static Date getMonthStartTime() {
        Calendar start = Calendar.getInstance();
        start.set(Calendar.DAY_OF_MONTH, 1);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        return start.getTime();
    }

    public static int getNowDay() {
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    public static int getNowMonth() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        System.out.println(month);
        return month + 1;
    }

    public static int getNowYear() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        return year;
    }

    /**
     * 现在年份减去输入年份差
     * @param date
     * @return
     */
    public static int getYearDifference(Date date) {
        Calendar cal = Calendar.getInstance();
        int nowYear = cal.get(Calendar.YEAR);
        cal.setTime(date);
        int otherYear = cal.get(Calendar.YEAR);
        return nowYear - otherYear;
    }

    public static Date getMonthStartTime(int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        return cal.getTime();
    }


    public static Date getMonthEndTime(int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);

        return cal.getTime();
    }

    public static String format(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    public static Date parseDate(String date, String format){
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date dateTime = null;
        try {
            dateTime = dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    /**
     * 字符串转日期
     * @param date
     * @return
     */
    public static Date stringToDate(String date,String format) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date simpleDate = null;
        simpleDate = simpleDateFormat.parse(date);
        return simpleDate;
    }


    /*public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal(1);
        Date addMonth = getAddMonth(bigDecimal);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentYeatTime = format.format(addMonth);
        System.out.println(currentYeatTime);
    }*/

    /**
     * 获取今年第一天00:00:00
     * @param
     * @return
     */
    public static Date getCurrentYearStartTime(){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH,0);//今年第一个月 月份置0

        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND,0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取上个月月份
     * @return
     */
    public static int getLastMonth(){
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MONTH);//上个月月份
    }

    /**
     * 获取当月第一天00:00:00
     * @param mouth
     * @return
     */
    public static Date getCurrentMouthEndTime(int mouth){
        Calendar c = Calendar.getInstance();
        int currentMouth = c.get(Calendar.MONTH) + 1;//当月月份
        int difference = mouth - currentMouth;
        c.add(Calendar.MONTH, difference);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND,0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取当月最后一天23:59:59
     * @param mouth
     * @return
     */
    public Date getCurrentMouthStartTime(int mouth){
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        int currentMouth = ca.get(Calendar.MONTH) + 1;//当月月份
        int difference = mouth - currentMouth;
        ca.add(Calendar.MONTH, difference);
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        ca.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        ca.set(Calendar.MINUTE, 59);
        //将秒至59
        ca.set(Calendar.SECOND,59);
        //将毫秒至999
        ca.set(Calendar.MILLISECOND, 999);
        return ca.getTime();
    }

    /**
     * 获取指定时间到格林威治时间的秒数
     * UTC：格林威治时间1970年01月01日00时00分00秒（UTC+8北京时间1970年01月01日08时00分00秒）
     * @return
     */
    public static long diffSeconds(){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1970,0,1);
        calendar.set(Calendar.HOUR,8);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        TimeZone timeZone = TimeZone.getTimeZone("GMT+08:00");
        calendar.setTimeZone(timeZone);
        Date time = calendar.getTime();
        String format = format(time, "yyyy-MM-dd HH:mm:ss");
        System.out.println(format);
        long old = calendar.getTimeInMillis() / 1000;
        long now = System.currentTimeMillis() / 1000;
        return now - old;
    }

    public static void main(String[] args) throws Exception {
/*        System.out.println(diffSeconds());
        System.out.println(System.currentTimeMillis()/1000);*/

    }

    //解析Excel日期格式
    public static String ExcelDoubleToDate(String strDate) {
        if(strDate.length() == 5){
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date tDate = DoubleToDate(Double.parseDouble(strDate));
                return sdf.format(tDate);
            }catch (Exception e){
                e.printStackTrace();
                return strDate;
            }
        }
        return strDate;
    }

    //解析Excel日期格式
    public static Date DoubleToDate(Double dVal) {
        Date tDate = new Date();
        long localOffset = tDate.getTimezoneOffset() * 60000; //系统时区偏移 1900/1/1 到 1970/1/1 的 25569 天
        tDate.setTime((long) ((dVal - 25569) * 24 * 3600 * 1000 + localOffset));

        return tDate;
    }
}
